package com.alameda.commonpent.context;

import org.springframework.context.ApplicationContext;

/**
 * spring装配组件
 *
 * @author Lv
 * @date 2024/5/1
 */
public class SpringContextHolder {
    /**
     * springboot运行容器
     */
    private static ApplicationContext applicationContext;

    /**
     * set注入
     *
     * @param applicationContext springboot运行容器
     */
    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }

    /**
     * 获取springboot运行容器
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过字节码获取当前spring容器中的bean对象
     *
     * @param tClass bean对象字节码
     * @param <T>    bean对象泛型
     * @return
     */
    public static <T> T getByClass(Class<T> tClass) {
        return getApplicationContext().getBean(tClass);
    }
}